home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 51 / Amiga Format CD51 (2000-03-10)(Future Publishing)(GB)[!][issue 2000-04].iso / -in_the_mag- / workbench / term_4.8 / extras / source / gtlayout-source.lha / LT_LevelWidth.c < prev    next >
C/C++ Source or Header  |  1997-05-08  |  10KB  |  337 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1997 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #include "Assert.h"
  15.  
  16.  
  17. /*****************************************************************************/
  18.  
  19.  
  20. STATIC LONG __stdargs
  21. DefaultDispFunc(struct Gadget *gad,LONG value)
  22. {
  23.     return(value);
  24. }
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30. VOID
  31. LTP_LevelWidth(LayoutHandle *handle,STRPTR levelFormat,DISPFUNC dispFunc,LONG min,LONG max,LONG *maxWidth,LONG *maxLen,BOOL fullCheck)
  32. {
  33.     UBYTE buffer[80];
  34.     LONG localMaxWidth;
  35.     LONG localMaxLen;
  36.     LONG len;
  37.  
  38.     localMaxWidth    = 0;
  39.     localMaxLen    = 0;
  40.  
  41.     if(handle)
  42.     {
  43.         if(!dispFunc)
  44.             dispFunc = (DISPFUNC)DefaultDispFunc;
  45.  
  46.         if(!levelFormat)
  47.         {
  48.             if(LocaleBase && LocaleBase->lb_SysPatches)
  49.                 levelFormat = "%lD";
  50.             else
  51.                 levelFormat = "%ld";
  52.         }
  53.  
  54.         if(fullCheck)
  55.         {
  56.             LONG i;
  57.  
  58.             for(i = min ; i <= max ; i++)
  59.             {
  60.                 SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,i));
  61.  
  62.                 len = strlen(buffer);
  63.  
  64.                 if(len > localMaxLen)
  65.                     localMaxLen = len;
  66.  
  67.                 len = TextLength(&handle->RPort,buffer,len);
  68.  
  69.                 if(len > localMaxWidth)
  70.                     localMaxWidth = len;
  71.             }
  72.         }
  73.         else
  74.         {
  75.             SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,min));
  76.  
  77.             len = strlen(buffer);
  78.  
  79.             if(len > localMaxLen)
  80.                 localMaxLen = len;
  81.  
  82.             len = TextLength(&handle->RPort,buffer,len);
  83.  
  84.             if(len > localMaxWidth)
  85.                 localMaxWidth = len;
  86.  
  87.             SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,max));
  88.  
  89.             len = strlen(buffer);
  90.  
  91.             if(len > localMaxLen)
  92.                 localMaxLen = len;
  93.  
  94.             len = TextLength(&handle->RPort,buffer,len);
  95.  
  96.             if(len > localMaxWidth)
  97.                 localMaxWidth = len;
  98.         }
  99.     }
  100.  
  101.     if(maxLen)
  102.         *maxLen = localMaxLen;
  103.  
  104.     if(maxWidth)
  105.         *maxWidth = localMaxWidth + handle->GlyphWidth;
  106. }
  107.  
  108.  
  109. /*****************************************************************************/
  110.  
  111.  
  112. /****** gtlayout.library/LT_LevelWidth ******************************************
  113. *
  114. *   NAME
  115. *    LT_LevelWidth -- Determine the maximum width of a SLIDER_KIND
  116. *                     level string.
  117. *
  118. *   SYNOPSIS
  119. *    Index = LT_LevelWidth(Handle,FormatString,DispFunc,Min,Max,MaxWidth,
  120. *      D0                    A0        A1         A2     D0  D1    A3
  121. *
  122. *                              MaxLen,FullCheck);
  123. *                                A5      D2
  124. *
  125. *    LONG LT_LevelWidth(LayoutHandle *,STRPTR,
  126. *                       LONG (*)(struct Gadget *,WORD),LONG,LONG,LONG *,
  127. *                       LONG *,BOOL);
  128. *
  129. *   FUNCTION
  130. *    In order to make room for the level text displayed by a
  131. *    SLIDER_KIND object one needs to know how much space the
  132. *    longest level string will occupy. Otherwise, the level
  133. *    text may overwrite the gadget label text or the slider
  134. *    container. This routine will rattle through all possible
  135. *    slider settings (as given via the Min and the Max
  136. *    level values) and determine the longest label string
  137. *    according to the font used.
  138. *
  139. *   INPUTS
  140. *    Handle - Pointer to a LayoutHandle structure, as returned by
  141. *        a call to LT_CreateHandleTags().
  142. *
  143. *    FormatString - The sprintf() style formatting string to be used
  144. *        to format the slider level settings into text.
  145. *        This is the same string you would pass in via
  146. *        the GTSL_LevelFormat tag when creating the
  147. *        slider object.
  148. *        Default: "%lD" for systems which have locale.library
  149. *            installed, "%ld" otherwise.
  150. *
  151. *    DispFunc - A pointer to the function to filter the slider level
  152. *        values. The result of this function will then be
  153. *        used to format a string into the slider level text.
  154. *        This is the same parameter you would pass in via
  155. *        the GTSL_DispFunc tag when creating the slider
  156. *        object.
  157. *
  158. *            NOTE: the routine will be called with a NULL Gadget
  159. *                parameter, make sure your code will handle
  160. *                this nicely.
  161. *
  162. *        Default: no display function
  163. *
  164. *    Min - The smallest value the slider can be set to. This is
  165. *        same value you would pass in via GTSL_Min when creating
  166. *        the slider object.
  167. *
  168. *    Max - The largest value the slider can be set to. This is
  169. *        same value you would pass in via GTSL_Max when creating
  170. *        the slider object.
  171. *
  172. *    MaxWidth - Pointer to a place to store the width of the
  173. *        longest level string in pixels. If you pass
  174. *        in NULL instead of the address of a variable
  175. *        no harm will be done.
  176. *
  177. *    MaxLen - Pointer to a place to store the length of the
  178. *        longest level string in characters. If you pass
  179. *        in NULL instead of the address of a variable
  180. *        no harm will be done.
  181. *
  182. *    FullCheck - TRUE will cause the code to rattle through all
  183. *        possible slider settings, starting from the
  184. *        minimum value, ending at the maximum value.
  185. *        While this may be a good idea for a display
  186. *        function to map slider levels to text strings
  187. *        of varying length it might be a problem when
  188. *        it comes to display a range of numbers from
  189. *        1 to 40,000: the code will loop through
  190. *        40,000 iterations trying to find the longest
  191. *        string.
  192. *
  193. *        FALSE will cause the code to calculate the
  194. *        longest level string based only on the
  195. *        minimum and the maximum value to check.
  196. *        While this is certainly a good a idea when
  197. *        it comes to display a range of numbers from
  198. *        1 to 40,000 as only two values will be
  199. *        checked the code may fail to produce
  200. *        accurate results for sliders using display
  201. *        functions mapping slider levels to strings.
  202. *
  203. *   RESULT
  204. *    Index - The slider level which gives the longest
  205. *        level string.
  206. *
  207. *   NOTES
  208. *    Some compilers have trouble passing parameters in A5. In such
  209. *    a case it is recommended to use gtlayout.library/LT_NewLevelWidth
  210. *    instead.
  211. *
  212. *   SEE ALSO
  213. *    gtlayout.library/LT_NewLevelWidth
  214. *
  215. ******************************************************************************
  216. *
  217. */
  218.  
  219. VOID LIBENT
  220. LT_LevelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR levelFormat,REG(a2) DISPFUNC dispFunc,REG(d0) LONG min,REG(d1) LONG max,REG(a3) LONG *maxWidth,REG(a5) LONG *maxLen,REG(d2) BOOL fullCheck)
  221. {
  222.     LTP_LevelWidth(handle,levelFormat,dispFunc,min,max,maxWidth,maxLen,fullCheck);
  223. }
  224.  
  225. /****** gtlayout.library/LT_NewLevelWidth ******************************************
  226. *
  227. *   NAME
  228. *    LT_NewLevelWidth -- Determine the maximum width of a SLIDER_KIND
  229. *                        level string. (V14)
  230. *
  231. *   SYNOPSIS
  232. *    Index = LT_LevelWidth(Handle,FormatString,DispFunc,Min,Max,MaxWidth,
  233. *      D0                    A0        A1         A2     D0  D1    A3
  234. *
  235. *                              MaxLen,FullCheck);
  236. *                                D3      D2
  237. *
  238. *    LONG LT_LevelWidth(LayoutHandle *,STRPTR,
  239. *                       LONG (*)(struct Gadget *,WORD),LONG,LONG,LONG *,
  240. *                       LONG *,BOOL);
  241. *
  242. *   FUNCTION
  243. *    In order to make room for the level text displayed by a
  244. *    SLIDER_KIND object one needs to know how much space the
  245. *    longest level string will occupy. Otherwise, the level
  246. *    text may overwrite the gadget label text or the slider
  247. *    container. This routine will rattle through all possible
  248. *    slider settings (as given via the Min and the Max
  249. *    level values) and determine the longest label string
  250. *    according to the font used.
  251. *
  252. *   INPUTS
  253. *    Handle - Pointer to a LayoutHandle structure, as returned by
  254. *        a call to LT_CreateHandleTags().
  255. *
  256. *    FormatString - The sprintf() style formatting string to be used
  257. *        to format the slider level settings into text.
  258. *        This is the same string you would pass in via
  259. *        the GTSL_LevelFormat tag when creating the
  260. *        slider object.
  261. *        Default: "%lD" for systems which have locale.library
  262. *            installed, "%ld" otherwise.
  263. *
  264. *    DispFunc - A pointer to the function to filter the slider level
  265. *        values. The result of this function will then be
  266. *        used to format a string into the slider level text.
  267. *        This is the same parameter you would pass in via
  268. *        the GTSL_DispFunc tag when creating the slider
  269. *        object.
  270. *
  271. *        NOTE: the routine will be called with a NULL Gadget
  272. *            parameter, make sure your code will handle
  273. *            this nicely.
  274. *
  275. *        Default: no display function
  276. *
  277. *    Min - The smallest value the slider can be set to. This is
  278. *        same value you would pass in via GTSL_Min when creating
  279. *        the slider object.
  280. *
  281. *    Max - The largest value the slider can be set to. This is
  282. *        same value you would pass in via GTSL_Max when creating
  283. *        the slider object.
  284. *
  285. *    MaxWidth - Pointer to a place to store the width of the
  286. *        longest level string in pixels. If you pass
  287. *        in NULL instead of the address of a variable
  288. *        no harm will be done.
  289. *
  290. *    MaxLen - Pointer to a place to store the length of the
  291. *        longest level string in characters. If you pass
  292. *        in NULL instead of the address of a variable
  293. *        no harm will be done.
  294. *
  295. *    FullCheck - TRUE will cause the code to rattle through all
  296. *        possible slider settings, starting from the
  297. *        minimum value, ending at the maximum value.
  298. *        While this may be a good idea for a display
  299. *        function to map slider levels to text strings
  300. *        of varying length it might be a problem when
  301. *        it comes to display a range of numbers from
  302. *        1 to 40,000: the code will loop through
  303. *        40,000 iterations trying to find the longest
  304. *        string.
  305. *
  306. *        FALSE will cause the code to calculate the
  307. *        longest level string based only on the
  308. *        minimum and the maximum value to check.
  309. *        While this is certainly a good a idea when
  310. *        it comes to display a range of numbers from
  311. *        1 to 40,000 as only two values will be
  312. *        checked the code may fail to produce
  313. *        accurate results for sliders using display
  314. *        functions mapping slider levels to strings.
  315. *
  316. *   RESULT
  317. *    Index - The slider level which gives the longest
  318. *        level string.
  319. *
  320. *   NOTES
  321. *    This function does exactly what gtlayout.library/LT_LevelWidth
  322. *    does, but uses a slightly different register ordering. Namely,
  323. *    the MaxLen pointer is passed in D3 instead of A5.
  324. *
  325. *   SEE ALSO
  326. *    gtlayout.library/LT_LevelWidth
  327. *
  328. ******************************************************************************
  329. *
  330. */
  331.  
  332. VOID LIBENT
  333. LT_NewLevelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR levelFormat,REG(a2) DISPFUNC dispFunc,REG(d0) LONG min,REG(d1) LONG max,REG(a3) LONG *maxWidth,REG(d3) LONG *maxLen,REG(d2) BOOL fullCheck)
  334. {
  335.     LTP_LevelWidth(handle,levelFormat,dispFunc,min,max,maxWidth,maxLen,fullCheck);
  336. }
  337.